package com.zou.service;

import com.zou.commons.constants.Constants;
import com.zou.commons.utils.DateUtils;
import com.zou.commons.utils.RemoveHtml;
import com.zou.commons.utils.UUIDUtils;
import com.zou.mapper.ArticleMapper;
import com.zou.pojo.Article;
import com.zou.pojo.Column;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("articleService")
public class ArticleService {
    @Autowired(required = false)
    private ArticleMapper articleMapper;

    /**
     * 保存创建的文章
     *
     * @param article
     * @return
     */
    public boolean saveArticle(Article article) {
        article.setArticleId(UUIDUtils.getUUID());
        // 设置序号默认最大
        try {
            article.setArticleNumber(String.format("%05d", Integer.valueOf(articleMapper.maxArticleNumber()) + 1));
        } catch (Exception e) {
            e.printStackTrace();
            article.setArticleNumber("00001");
        }
        // 默认文章浏览量为0
        article.setArticlePageViews("0");
        // 设置文章创建时间
        article.setArticleCreationDate(DateUtils.formatDate(new Date()));
        // 默认状态禁用/未发布
        article.setArticleState(Constants.STATE_DISABLED);
        // 默认
        try {
            //调用mapper层方法，保存创建的栏目
            if (articleMapper.insertArticle(article) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据条件分页查询文章
     *
     * @param map
     * @return
     */
    public List<Article> queryArticleByConditionForPage(Map<String, Object> map) {
        List<Article> articles = articleMapper.selectArticleByConditionForPage(map);
        // 去除html标签，并截取前十个字符
        for (Article article : articles) {
            String articleContent = RemoveHtml.removeHtmlTag(article.getArticleContent());
            if (articleContent.length() > 10) {
                articleContent = articleContent.substring(0, 10);
            }
            article.setArticleContent(articleContent);
        }
        return articles;
    }

    /**
     * 根据条件查询页数
     *
     * @param map
     * @return
     */
    public int queryCountOfArticleByCondition(Map<String, Object> map) {
        return articleMapper.selectCountOfArticleByCondition(map);
    }

    /**
     * 根据Id数组批量删除文章
     *
     * @param ids
     * @return
     */
    public boolean deleteArticleByIds(String[] ids) {
        try {
            //调用mapper层方法，删除栏目
            if (articleMapper.deleteArticleByIds(ids) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据ID查询文章
     *
     * @param id
     * @return
     */
    public Article queryArticleById(String id) {
        Article article = new Article();
        article.setArticleId(id);
        article = articleMapper.selectSomeArticleByArticle(article).get(0);
        return article;
    }

    /**
     * 修改文章
     *
     * @param article
     * @return
     */
    public boolean editArticle(Article article) {
        try {
            //调用mapper层方法，修改栏目
            if (articleMapper.updateArticle(article) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 置顶文章
     *
     * @param id
     * @return
     */
    public boolean stickyArticle(String id) {
        Article article = new Article();
        article.setArticleId(id);
        article.setArticleNumber(String.format("%05d", Integer.valueOf(articleMapper.maxArticleNumber()) + 1));
        try {
            //调用mapper层方法，修改栏目
            if (articleMapper.updateArticle(article) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 前台根据栏目id查询启用的标题和id
     *
     * @param id
     * @return
     */
    public List<Article> queryTitleAndIdByColumnId(String id) {
        return articleMapper.selectTitleAndIdByColumnId(id);
    }

    /**
     * 查询五个浏览量最多的文章
     *
     * @return
     */
    public List<Article> queryFivePageViewsMaxArticle() {
        return articleMapper.selectFivePopularTitleAndId();
    }

    /**
     * 异步执行文章浏览量加一
     *
     * @param id
     */
    @Async
    public void articlePageViewsPlusOne(String id) {
        Article article = new Article();
        article.setArticleId(id);
        // 获取当前文章的浏览量
        String articlePageViews = articleMapper.selectSomeArticleByArticle(article).get(0).getArticlePageViews();
        article.setArticlePageViews(String.format("%08d", Integer.valueOf(articlePageViews) + 1));
        // 修改文章
        articleMapper.updateArticle(article);
    }
}
